/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             LogService.svc.cs
*        创建系统时间:       2012/11/2 19:07:05
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class LogService : MarshalByRefObject, ILogService
    {
        private static LogService instance = null;
        private static object locker = new object();

        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseLogDao(dbHelper, userInfo).BatchDelete(ids);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int Delete(BaseUserInfo userInfo, string id)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseLogDao(dbHelper, userInfo).Delete(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable GetListByDate(BaseUserInfo userInfo, string beginDate, string endDate)
        {
            DataTable table = new DataTable(BaseLogTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseLogDao dao = new BaseLogDao(dbHelper, userInfo);
                if (userInfo.IsAdministrator)
                {
                    table = dao.GetDTByDate(string.Empty, string.Empty, beginDate, endDate);
                }
                else
                {
                    string[] userIDs = new BaseResourcePermissionScopeDao(dbHelper, userInfo).GetUserIDs(userInfo.ID, "SystemAdmin");
                    table = dao.GetDTByDateByUserIDs(userIDs, string.Empty, string.Empty, beginDate, endDate);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetListByModule(BaseUserInfo userInfo, string processID, string beginDate, string endDate)
        {
            DataTable table = new DataTable(BaseLogTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseLogDao dao = new BaseLogDao(dbHelper, userInfo);
                if (userInfo.IsAdministrator)
                {
                    table = dao.GetDTByDate(BaseLogTable.FieldProcessID, processID, beginDate, endDate);
                }
                else
                {
                    string[] userIDs = new BaseResourcePermissionScopeDao(dbHelper, userInfo).GetUserIDs(userInfo.ID, "SystemAdmin");
                    table = dao.GetDTByDateByUserIDs(userIDs, BaseLogTable.FieldProcessID, processID, beginDate, endDate);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetListByUser(BaseUserInfo userInfo, string userID, string beginDate, string endDate)
        {
            DataTable table = new DataTable(BaseLogTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseLogDao(dbHelper, userInfo).GetDTByDate(BaseLogTable.FieldUserID, userID, beginDate, endDate);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetLogGeneral(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseLogTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
            try
            {
                dbHelper.Open();
                if (userInfo.IsAdministrator)
                {
                    dT = dao.GetDT();
                }
                else
                {
                    string[] userIDs = new BaseResourcePermissionScopeDao(dbHelper, userInfo).GetUserIDs(userInfo.ID, "SystemAdmin");
                    dT = dao.GetDTByIDs(userIDs);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public void Load()
        {
        }

        public DataTable ResetVisitInfo(BaseUserInfo userInfo, string[] ids)
        {
            DataTable dT = new DataTable(BaseLogTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dbHelper.BeginTransaction();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                dao.ResetVisitInfo(ids);
                dT = dao.GetDT();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
                dbHelper.CommitTransaction();
            }
            catch (Exception exception)
            {
                dbHelper.RollbackTransaction();
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable Truncate(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseLogTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseLogDao dao = new BaseLogDao(dbHelper, userInfo);
                dao.Truncate();
                dT = dao.GetDT();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public void WriteLog(BaseUserInfo userInfo, string processID, string processName, string methodName)
        {
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseLogDao.Instance.Add(dbHelper, userInfo, processID, processName, methodName);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
        }

        public static LogService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new LogService();
                        }
                    }
                }
                return instance;
            }
        }
    }
}